/* 定义一个规则
*
* 用作登录和注册时候的js代码
* 验证码 4位
* 用户名5-15位
* 密码6-20位
* 确认密码要和密码相同
* email符合邮箱规范  ^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$
* 姓名校验 不为空
* 手机号验证   /^1([38]\d|5[0-35-9]|7[3678])\d{8}$/;
*
* */

//验证手机号
function checkPhone() {
    //获取username
    let telephone = $("#telephone").val();
    //定义正则
    let reg_telephone = /^1([38]\d|5[0-35-9]|7[3678])\d{8}$/;

    let flag = reg_telephone.test(telephone);
    //判断是否符合正则
    if(flag){
        $("#password2").css("border","2px solid green");
        $("#message").text("");
    }else{
        $("#password2").css("border","1px solid red");
        $("#message").text("请输入有效的手机号");
    }
    return flag;
}


//校验姓名
function checkName() {
    //获取username
    let name = $("#name").val();
    //定义正则
    let reg_name = /^\w*$/;

    let flag = !reg_name.test(name);
    //判断是否符合正则
    if(flag){
        $("#name").css("border","2px solid green");
        $("#message").text("");
    }else{
        $("#name").css("border","1px solid red");
        $("#message").text("请输入姓名");
    }
    return flag;
}

//校验邮箱
function checkEmail() {
    //获取username
    let email = $("#email").val();
    //定义正则
    let reg_email = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;

    let flag = reg_email.test(email);
    //判断是否符合正则
    if(flag){
        $("#email").css("border","2px solid green");
        $("#message").text("");
    }else{
        $("#email").css("border","1px solid red");
        $("#message").text("邮箱格式不正确");
    }
    return flag;
}

//校验确认密码
function checkPassword2() {
    //获取username
    let password2 = $("#password2").val();
    //定义正则
    // let reg_password2 = /^\w{6,20}$/;

    let password = $("#password").val();
    let flag = password===password2;
    //判断是否符合正则
    if(flag){
        $("#password2").css("border","2px solid green");
        $("#message").text("");
    }else{
        $("#password2").css("border","1px solid red");
        $("#message").text("两次密码不一致");
    }
    return flag;
}

//校验密码
function checkPassword() {
    //获取username
    let password = $("#password").val();
    //定义正则
    let reg_password = /^\w{6,20}$/;
    let flag = reg_password.test(password);
    //判断是否符合正则
    if(flag){
        $("#password").css("border","2px solid green");
        $("#message").text("");
    }else{
        $("#password").css("border","1px solid red");
        $("#message").text("请输入6-20位密码");
    }
    return flag;
}


//检验用户名
function checkUsername() {
    //获取username
    let username = $("#username").val();
    //定义正则
    let reg_username = /^\w{5,15}$/;
    let flag = reg_username.test(username);
    //判断是否符合正则
    if(flag){
        $("#username").css("border","2px solid green");
        $("#message").text("");
    }else{
        $("#username").css("border","1px solid red");
        $("#message").text("请输入5-15位用户名");
    }
    return flag;
}


//校验验证码
function checkCheck() {
    //1.获取验证码
    const checkCode = $("#checkCode").val();
    //2.定义正则		只能是四位字符
    const reg_checkCode =  /^\w{4}$/;
    //3.判断
    const flag = reg_checkCode.test(checkCode);
    if(flag){
        $("#checkCode").css("border","2px solid green");
        $("#message").text("");
    }else{
        $("#checkCode").css("border","1px solid red");
        $("#message").text("请输入4位有效的验证码");
    }
    return flag;
}


//图片点击事件，用于刷新验证码
function changeCheckCode(img) {
    img.src="/verifyCode?time="+new Date().getTime();
}
